<script setup>
import pageContainer from '@/components/pageContainer.vue';
import { ref } from 'vue';
import { ElMessage, ElLoading } from 'element-plus';
import {userPasswordService} from '@/api/user'
import { useUserStore } from '@/stores';
import { useRouter } from 'vue-router';
// 绑定数据
const userStore=useUserStore()
const router=useRouter()
const form = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
});
const formRef=ref()
const rules = {
  old_pwd: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' },
    { validator: (rule, value, callback)=>{
        if(value===form.value.old_pwd){
            callback(new Error('新旧密码设计一致，请重新设计新密码'))
        }
        else{
            callback()
        }
    } ,trigger:'change'}
  ],
  re_pwd: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    { validator: (rule, value, callback) => {
        if (value !== form.value.new_pwd) {
          callback(new Error('两次输入的新密码不一致'));
        } else {
          callback();
        }
      }, trigger: 'change' }
  ]
};
//函数
const resetForm = () => {
     formRef.value.resetFields();
};
const submitForm = async () => {
    let loadingInstance;
    try {
        await formRef.value.validate();
        loadingInstance = ElLoading.service({
            lock: true,
            text: '正在提交...',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        await userPasswordService(form.value);
        ElMessage.success('密码已修改成功，稍后为你跳转');
        setTimeout(() => {
            loadingInstance.close();
            router.push('/login');
        }, 2000);
    } catch (error) {
        if (loadingInstance) loadingInstance.close();
        ElMessage.error('密码修改失败，请重试');
        console.error(error);
    }
};

</script>

<template>
  <pageContainer title="重置密码">
    <div class="formPassword">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="原密码" prop="old_pwd">
          <el-input v-model="form.old_pwd" type="password" placeholder="请输入原密码" />
        </el-form-item>
        <el-form-item label="新密码" prop="new_pwd">
          <el-input v-model="form.new_pwd" type="password" placeholder="请输入新密码" />
        </el-form-item>
        <el-form-item label="确认新密码" prop="re_pwd">
          <el-input v-model="form.re_pwd" type="password" placeholder="请确认新密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">修改密码</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </pageContainer>
</template>

<style scoped>
.formPassword {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  margin-right: 10px;
}
</style>